package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Suppliers;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Suppliers DAO
 * 
 * @author Administrator
 * 
 */
public class SuppliersDao {

	DBHelper dbHelper;

	private final static String LOG_TAG = "SuppliersDao";
	protected static final String TBL_NAME = "suppliers";

	public SuppliersDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new suppliers
	 * 
	 * @param suppliers
	 */
	public String save(Suppliers suppliers) {
		Log.i(LOG_TAG, "SuppliersDao  save " + suppliers.toString());
		suppliers.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		suppliers.setCreated(time);
		suppliers.setModified(time);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into suppliers(ID,Company,Address,Phone,ContactPerson,Mobile,created,modified) values(?,?,?,?,?,?,?,?)",
					new Object[] { suppliers.getId(), suppliers.getCompany(),
							suppliers.getAddress(), suppliers.getPhone(),
							suppliers.getContactPerson(),
							suppliers.getMobile(), suppliers.getCreated(),
							suppliers.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("SuppliersDao  save", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
		return suppliers.getId();
	}

	/**
	 * update a suppliers
	 * 
	 * @param suppliers
	 */

	public void update(Suppliers suppliers) {
		Log.i(LOG_TAG, "SuppliersDao  update " + suppliers.toString());
		suppliers.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"update suppliers set Company=?,Address=?,Phone=?,ContactPerson=?,Mobile=?,modified=? where ID=?",
					new Object[] { suppliers.getCompany(),
							suppliers.getAddress(), suppliers.getPhone(),
							suppliers.getContactPerson(),
							suppliers.getMobile(), suppliers.getModified(),
							suppliers.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("SuppliersDao  update", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
	}

	/**
	 * get a suppliers by id
	 * 
	 * @param i
	 * @return
	 */
	public Suppliers find(String id) {
		Log.i(LOG_TAG, "SuppliersDao  find " + id);
		Suppliers suppliers = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,Company,Address,Phone,ContactPerson,Mobile,created,modified from suppliers where ID=?",
						new String[] { id });
		if (cursor.moveToNext()) {
			suppliers = new Suppliers();

			suppliers.setId(cursor.getString(cursor.getColumnIndex("ID")));
			suppliers.setCompany(cursor.getString(1));
			suppliers.setAddress(cursor.getString(2));
			suppliers.setPhone(cursor.getString(3));
			suppliers.setContactPerson(cursor.getString(4));
			suppliers.setMobile(cursor.getString(5));
			suppliers.setCreated(cursor.getLong(6));
			suppliers.setModified(cursor.getLong(7));
			
		}
		cursor.close();
		dbHelper.close();
		return suppliers;
	}

	/**
	 * delete a suppliers by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		Log.i(LOG_TAG, "SuppliersDao  delete " + id);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from suppliers where ID=?", new Object[] { id });
		dbHelper.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "  delete all from  suppliers: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete  from suppliers");
		dbHelper.close();
	}

	/**
	 * fetch supplierss
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Suppliers> getScrollData(int pageNumber, int pageSize) {

		Log.i(LOG_TAG, "SuppliersDao  getScrollData " + pageNumber + ","
				+ pageSize);

		List<Suppliers> supplierss = new ArrayList<Suppliers>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,Company,Address,Phone,ContactPerson,Mobile,created,modified from suppliers order by modified desc limit ?,?",
						new String[] {
								String.valueOf((pageNumber - 1) * pageSize),
								String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Suppliers suppliers = new Suppliers();
			suppliers.setId(cursor.getString(cursor.getColumnIndex("ID")));
			suppliers.setCompany(cursor.getString(1));
			suppliers.setAddress(cursor.getString(2));
			suppliers.setPhone(cursor.getString(3));
			suppliers.setContactPerson(cursor.getString(4));
			suppliers.setMobile(cursor.getString(5));
			suppliers.setCreated(cursor.getLong(6));
			suppliers.setModified(cursor.getLong(7));
			supplierss.add(suppliers);
		}
		cursor.close();
		dbHelper.close();
		return supplierss;
	}

	/**
	 * fetch all supplierss
	 * 
	 * @return
	 */
	public List<Suppliers> getAll() {

		Log.i(LOG_TAG, "SuppliersDao  getScrollData ");

		List<Suppliers> supplierss = new ArrayList<Suppliers>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,Company,Address,Phone,ContactPerson,Mobile,created,modified from suppliers order by modified desc",
						null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Suppliers suppliers = new Suppliers();
			suppliers.setId(cursor.getString(cursor.getColumnIndex("ID")));
			suppliers.setCompany(cursor.getString(1));
			suppliers.setAddress(cursor.getString(2));
			suppliers.setPhone(cursor.getString(3));
			suppliers.setContactPerson(cursor.getString(4));
			suppliers.setMobile(cursor.getString(5));
			suppliers.setCreated(cursor.getLong(6));
			suppliers.setModified(cursor.getLong(7));
			supplierss.add(suppliers);
		}
		cursor.close();
		dbHelper.close();
		return supplierss;
	}

	/**
	 * get suppliers's count
	 * 
	 * @return
	 */
	public long getCount() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		dbHelper.close();
		return count;
	}

}
